import Vue from 'vue'
import Router from 'vue-router'
import Find from '@/components/find.vue'
import Login from '@/components/login.vue'
import Detail from '@/components/self-detail.vue'
import Search from '@/components/search.vue'

Vue.use(Router)

const router= new Router({
  routes: [
    {
      path: '/find',
      component: Find
    },
    {
      path: '/',
      redirect:'/find'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/detail/:id',
      component: Detail
    },
    {
      path: '/search',
      component: Search
    }
  ]
})


//a=挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to 将要访问的路径
  if(to.path==='/login') return next()
  //获取cookie
  const cookie =window.sessionStorage.getItem('cookie')
  if(!cookie) return next('/login')
  next()
  //from 哪个路径而来
  //next 放行函数 强制跳转
})
export default router
